<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script>
        var calcType = prompt('1加法, 2减法, 3乘法, 4除法');
        console.log(calcType, typeof calcType); // '1', 'string'

        if (calcType != '1' && calcType != '2' && calcType != '3' && calcType != '4') {
            alert('输入错误请重新输入');
            // return;
        } else {
            var n1 = prompt('第一个数字');
            var n2 = prompt('第二个数字');

            check(n1);
            check(n2);

            switch (calcType) {
                case '1':
                    var result = Number(n1) + Number(n2);
                    alert(result);
                    break;
                case '2':
                    var result = Number(n1) - Number(n2);
                    alert(result);
                    break;
                case '3':
                    var result = Number(n1) * Number(n2);
                    alert(result);
                    break;
                case '4':
                    var result = Number(n1) / Number(n2);
                    alert(result);
                    break;

                default:
                    break;
            }
        }

        function check (value) {
            if (!Number(value) && Number(value) !== 0) {
                alert('输入错误请重新输入');
                return false;
            }
            return true;
        }


    </script>
</body>
</html>